package com.xoa.service.party;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.party.Execute_ControlMapper;
import com.xoa.dao.party.PointMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.party.Execute_Control;
import com.xoa.model.party.Point;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by crr on 2018/4/20.
 */
@Service
public class ExecuteControlService {
    @Autowired
    Execute_ControlMapper executeControlMapper;
    @Autowired
    SysCodeMapper sysCodeMapper;

    @Autowired
    PointMapper pointMapper;

    /**
     * 郭心雨
     * 2018-4-20
     * 获取全部的布控点信息
     * @return
     */
    public ToJson<Execute_Control> getAllExecute(String dispa, String gridId , Integer page, Integer pageSize) {
        ToJson<Execute_Control> toJson=new ToJson<Execute_Control>();
        Map<String, Object> param = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        param.put("page", pageParams);
        param.put("dispa", dispa);
        param.put("gridId", gridId);
        List<Execute_Control> conList=executeControlMapper.getAllExecute(param);
        if (conList!=null&&conList.size()>0){
            for(Execute_Control control:conList){
                Map<String,Object> map=new HashMap<>();
                map.put("conId",control.getConId());
                control.setDispaName( sysCodeMapper.getSingleCode("org_dispa",control.getDispa()).getCodeName());
                control.setGridName(sysCodeMapper.getSingleCode("TYPE_OF_ACCOUNT",control.getGridInfo()).getCodeName());
                List<Point> pointList=pointMapper.getPointByConId(map);
                if(pointList!=null&&pointList.size()>0){
                    control.setPoint("0");
                }else {
                    control.setPoint(String.valueOf(pointList.size()));
                }
                control.setPointList(pointList);
            }

            toJson.setTotleNum(pageParams.getTotal());
            toJson.setObj(conList);
            toJson.setMsg("ok");
        }else {
            toJson.setMsg("err");
        }
        return toJson;
    }

    /**
     * 郭心雨
     * 2018.4.20
     * 根据id查看布控点详情
     * @param conId
     * @return
     */
    public ToJson<Execute_Control> getExecuteById(Integer conId) {
        ToJson<Execute_Control> toJson=new ToJson<Execute_Control>();

        Execute_Control executeControl=executeControlMapper.getExecuteById(conId);
        if (executeControl!=null){
            executeControl.setDispaName( sysCodeMapper.getSingleCode("org_dispa",executeControl.getDispa()).getCodeName());
            executeControl.setGridName(sysCodeMapper.getSingleCode("TYPE_OF_ACCOUNT",executeControl.getGridInfo()).getCodeName());
            toJson.setObject(executeControl);
            toJson.setMsg("ok");
        }else {
            toJson.setMsg("没有信息");
        }
        return toJson;
    }

    /**
     * 郭心雨
     * 2018.4.20
     * 根据id修改布控信息
     * @return
     */
    public ToJson updateExecuteById(Execute_Control executeControl) {
        ToJson toJson=new ToJson();
        int count=executeControlMapper.updateByPrimaryKeySelective(executeControl);
        if (count!=0){
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setMsg("err");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 郭心雨
     * 2018.4.20
     * 添加布控信息
     * @param executeControl
     * @return
     */
    public ToJson addExecute(Execute_Control executeControl) {
        ToJson toJson=new ToJson();
        int count=executeControlMapper.insertSelective(executeControl);
        if (count!=0){
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setMsg("err");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 郭心雨
     * 2018.4.20
     * 删除布控信息
     * @param conId
     * @return
     */
    public ToJson delExecute(Integer conId) {
        ToJson toJson=new ToJson();
        int count=executeControlMapper.deleteByPrimaryKey(conId);
            if (count>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else{
                toJson.setMsg("err");
                toJson.setFlag(1);
            }

         return toJson;
    }

    /**
     * 郭心雨
     * 2018.4.20
     * 获取列表信息
     * @param parentNo
     * @return
     */
    public ToJson getAllGridInfo(String parentNo) {
        ToJson toJson=new ToJson();
     //   List<SysCode> list=sysCodeMapper.getParentNo(parentNo);
    //    toJson.setObj(list);
        return toJson;
    }


    /**
     * 郭心雨
     * 2018-4-20
     * 获取全部的布控点信息（分组）
     * @return
     */
    public ToJson<Execute_Control> getAllExecuteNew(String dispa, Integer page, Integer pageSize) {
        ToJson<Execute_Control> toJson=new ToJson<Execute_Control>();
        Map<String, Object> param = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        param.put("page", pageParams);
        param.put("dispa", dispa);
        List<Execute_Control> conList=executeControlMapper.getAllExecuteNew(param);
        if (conList!=null&&conList.size()>0){
            for(Execute_Control control:conList){
                Map<String,Object> map=new HashMap<>();
                map.put("conId",control.getConId());
                control.setDispaName( sysCodeMapper.getSingleCode("org_dispa",control.getDispa()).getCodeName());
                control.setGridName(sysCodeMapper.getSingleCode("TYPE_OF_ACCOUNT",control.getGridInfo()).getCodeName());
                List<Point> pointList=pointMapper.getPointByConId(map);
                if(pointList!=null&&pointList.size()>0){
                    control.setPoint("0");
                }else {
                    control.setPoint(String.valueOf(pointList.size()));
                }
                control.setPointList(pointList);
            }

            toJson.setTotleNum(pageParams.getTotal());
            toJson.setObj(conList);
            toJson.setMsg("ok");
        }else {
            toJson.setMsg("err");
        }
        return toJson;
    }
}
